package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static org.springframework.beans.BeanUtils.copyProperties;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //判断当前加入到购物车里的商品是否已经存在了
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> cart = shoppingCartMapper.getByDishIdOrSetmealId(shoppingCart);
        //如果已经存在了，只需将数量加一
        if (cart != null && cart.size() > 0) {
            ShoppingCart cart1 = cart.get(0);
            cart1.setNumber(cart1.getNumber() + 1);
            shoppingCartMapper.update(cart1);
            return;
        }else{
            //如果不存在，需要插入一条购物车数据
            //判断添加的商品是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.getStatusById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long currentId = BaseContext.getCurrentId();

        ShoppingCart  shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(currentId);

        List<ShoppingCart> cart = shoppingCartMapper.getByDishIdOrSetmealId(shoppingCart);
        return cart;
    }

    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {

        //查看购物车商品数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> cart = shoppingCartMapper.getByDishIdOrSetmealId(shoppingCart);
        //如果商品数量为1，则删除该商品
        Integer number = cart.get(0).getNumber();
        if(number == 1){
            shoppingCartMapper.delete(shoppingCart);
            //更新购物车显示
        }else{
            //如果商品数量大于1，则将数量减一
            shoppingCart.setNumber(number-1);
            shoppingCartMapper.updateNumber(shoppingCart);
        }
    }
}
